package com.sooncode.project.focusservice.service;

import com.alibaba.fastjson.JSON;
import com.sooncode.project.core.finder.*;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.focusservice.model.Organization;
import com.sooncode.project.focusservice.model.Test;
import com.sooncode.project.focusservice.model.TestData;
import com.sooncode.project.springcloudcommon.model.AuthInfo;
import com.sooncode.project.springcloudcommon.utils.DateUtils;
import com.sooncode.project.springcloudcommon.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TestService {
    public boolean add(Map map) {
        Test test = new Test();
        test.add(map);
        return true;
    }

    public boolean modify(String id, Map map) {
        Test test = getById(id);
        test.modify(map);
        return true;
    }

    public boolean delete(String id) {
        Test test = getById(id);
        test.delete();
        return true;
    }

    public Page<Test> getPageList(int pageSize, int pageIndex, Map map) {
        Object orgName = map.get("orgName");
        map.remove("orgName");
        Object name = map.get("name");
        map.remove("name");
        IFindWrapper<Test> finder = new Finder<>(Test.class)
            .byField("orgName", orgName, OType.contains)
            .and("name", name, OType.contains);
        String date = (String) map.get("testDate");
        if (StringUtils.isNotEmpty(date)) {
            String[] dateArr = date.split(",");
            Date date1 = DateUtils.parseDate(dateArr[0] + " 00:00:00");
            Date date2 = DateUtils.parseDate(dateArr[1] + " 23:59:59");
            finder = finder.and("testDate", date1, OType.gte)
                .and("testDate", date2, OType.lte);
            map.remove("testDate");
        }
        return finder.and(map)
            .page(pageSize, pageIndex, Sort.DESC("testDate"));
    }

    public Test getById(String id) {
        return new Finder<>(Test.class).byId(id);
    }

    public Object addTest(Map params) {
        AuthInfo authInfo = UserUtils.getCurrentUser();
        Organization org = new Finder<>(Organization.class)
            .byField("accountId", authInfo.getUserid()).first();
        if (org == null) throw new DomainException("机构不存在");

        org.handerTest();
        String data = JSON.toJSON(params.get("data")).toString();
        params.put("data", JSON.parseArray(data, TestData.class));
        params.put("orgId", org.getId());
        Test test = new Test();
        test.add(params);
        Map map = new LinkedHashMap();
        map.put("averageScore", String.format("%.2f", test.getAverageScore()));
        List<TestData> list = test.getData();
        list.sort((o1, o2) -> o1.getItem_no().compareTo(o2.getItem_no()));
        for (TestData item : list) {
            map.put("score" + item.getItem_no(), item.getItemName() + ":" + item.getLevel());
        }
        return map;
    }

    public Page<Test> getPageListForDevice(int pageSize, int pageIndex, Map map) {
        AuthInfo authInfo = UserUtils.getCurrentUser();
        Organization org = new Finder<>(Organization.class)
            .byField("accountId", authInfo.getUserid()).first();
        if (!map.containsKey("memberId")) map.put("last", true);
        return new Finder<>(Test.class)
            .byField("orgId", org.getId())
            .and(map)
            .page(pageSize, pageIndex, Sort.DESC("testDate"));
    }

    public Test getByPid(String id) {
        return new Finder<>(Test.class).byField("pid", id).first();
    }

    public List<Test> getCheckTestByPid(String id) {
        Test tempTest = getByPid(id);
        List<Test> testList = new Finder<>(Test.class)
            .byField("memberId", tempTest.getMemberId())
            .top(2, Sort.DESC("testDate"));
        return testList;
    }
}
